package com.lzh.sort;

import java.util.*;


public class demo {

    public static void main(String[] args) {
        demo demo = new demo();
        String s = demo.solve2("99999999999999999", "3");
        System.out.println(s);

    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 计算两个数之和
     *
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve(String s, String t) {
        // write code here

        String longer = s;
        String shorter = t;
        int carry = 0;
        boolean isDiff = false; // 记录两个字符串是否等长，如果等长为true，否则为false
        StringBuilder sb = new StringBuilder();
        if (s.length() > t.length()) {
            longer = s;
            shorter = t;
        } else if (s.length() < t.length()) {
            longer = t;
            shorter = s;
        } else {
            longer = s;
            shorter = t;
            isDiff = true;
        }
        int i, j;
        if ("".equals(shorter)) {
            i = 0;
        }
        if ("".equals(longer)) {
            j = 0;
        }
        for (i = shorter.length() - 1, j = longer.length() - 1; i >= 0; i--, j--) {
            int c1 = shorter.charAt(i) - '0';
            int c2 = longer.charAt(j) - '0';
            int cur = c1 + c2 + carry;
            if (cur > 9) {
                carry = 1;
                cur = cur - 10;
            } else {
                carry = 0;
            }
            sb.insert(0, cur);
        }
        while (!isDiff) { // 如果不是等长
            while (j >= 0) {
                int c = longer.charAt(j) - '0';
                int cur = c + carry;
                if (cur > 9) {
                    carry = 1;
                    cur = cur - 10;
                } else {
                    carry = 0;
                }
                sb.insert(0, cur);
                j--;
            }
        }
        if (carry == 1) {
            sb.insert(0, 1);
        }
        return sb.toString();
    }

    public String solve2(String s, String t) {
        int i = s.length() - 1, j = t.length() - 1;

        int temp = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0 || temp != 0) {
            temp += i >= 0 ? s.charAt(i--) - '0' : 0;
            temp += j >= 0 ? t.charAt(j--) - '0' : 0;
            sb.append(temp % 10);
            temp = temp / 10;
        }
        return sb.reverse().toString();
    }

}